package com.td.client;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.td.client.states.State;
import com.td.client.states.State.Type;
import com.td.core.communication.Communication;
import com.td.core.communication.MainReciever;
import com.td.core.communication.Sendable;
import com.td.core.communication.obj.Message;
import com.td.core.communication.obj.PreGameInfo;
import com.td.core.obj.TDGame;
import com.td.core.obj.TDMap;

public class MainGameWindow extends BasicGame implements MainReciever,
	TDMap.Screen {
	
	private PreGameInfo preGameInfo;
	private TDGame game;
	
	private State currentState;
	
	private Communication communication;
	
	private GameContainer gameContainer;
	
	public MainGameWindow() {
		super("Tower defense");

		preGameInfo = new PreGameInfo();
		game = new TDGame(8);
	}	
	
	public TDGame getGame() {
		return game;
	}


	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		currentState.render(gc, g);
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		
//		//ladowanie zasobow
//		if (!ResourceManager.RESOURCES_INITIATED){
//			try {
//				ResourceManager.loadResources("data/resources.xml");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		ResourceManager.RESOURCES_INITIATED = true;
//		}
		
		gameContainer = gc;
		setState(Type.START);
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		currentState.update(gc, delta);
	}

	@Override
	public boolean closeRequested() {
		if(communication != null) {
			communication.send(new Message(Message.EXIT, ""));
			//communication.end();
		}
		return super.closeRequested();
	}

	public void setState(Type type) {
				
		switch (type) {
		
//		case START:
//			currentState = new StartState(this, getGameContainer());
//			break;
//		case WAIT:
//			currentState = new WaitState(this, getGameContainer());
//			break;
//		case GAMEPLAY:
//			currentState = new GameplayState(this, getGameContainer());
//			break;
//		case STATS:
//			currentState = new StatsState(this, getGameContainer());
//			break;	
		
		}
	}

	public Communication getCommunication() {
		return communication;
	}

	public void setCommunication(Communication communication) {
		this.communication = communication;
	}
	
	public PreGameInfo getPreGameInfo() {
		return preGameInfo;
	}

	public void setPreGameInfo(PreGameInfo preGameInfo) {
		this.preGameInfo = preGameInfo;
	}

	public GameContainer getGameContainer() {
		return gameContainer;
	}

	public void setGameContainer(GameContainer gameContainer) {
		this.gameContainer = gameContainer;
	}

	@Override
	public void recieve(Sendable<?> obj) {
		
		if(obj instanceof PreGameInfo) {
			PreGameInfo temp = (PreGameInfo) obj;
			temp.copyTo(preGameInfo);
			return;
		}
		
		if(obj instanceof TDGame) {
			TDGame temp = (TDGame) obj;
			temp.copyTo(game);
			return;
		}
		
		if(obj instanceof Message) {
			Message msg = (Message) obj;
			switch(msg.getNumber()) {
				
			case Message.PRE_GAME_INFO_REQUEST:
				getCommunication().send(getPreGameInfo().getMyPlayer().getCopy());
				break;
			case Message.START_GAME:
				setState(Type.GAMEPLAY);
				break;	
				
			case Message.EXIT:
				//TODO
				System.out.println("Server closed.");
				getCommunication().end();
				System.exit(0);
				break;
			default:
				break;
			}
		}
	}
	
	public static void main(String[] args) throws SlickException {
		
		AppGameContainer app = new AppGameContainer(new MainGameWindow());
    	
		app.setTargetFrameRate(50);
    	app.setDisplayMode(X, Y, false);
		//app.setFullscreen(true);
    	app.start();
	}
}
